package com.pand.controller;

import com.pand.controller.common.BaseController;
import com.pand.service.CustomerService;
import com.pand.util.Response;
import com.pand.vo.CustomerVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * Created by lh on 2017/11/3.
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {
    @Autowired
    CustomerService customerService;

    private int activeAmount = 82076;

    @ApiOperation(value = "用户信息")
    @GetMapping("/info")
    public Response info(HttpServletRequest request) {
        long registerAmount = customerService.getRegisterCustomerAmount();
        long certifationAmount = customerService.getCertificationAmount();
        CustomerVo vo = new CustomerVo();
        vo.setRegisterAmount(getFinalNumeral(registerAmount));
        vo.setCertificationAmount(getFinalNumeral(certifationAmount));
        if (request.getSession().getAttribute("activeAmount") != null) {
            activeAmount = Integer.valueOf(request.getSession().getAttribute("activeAmount").toString());
        } else {
            activeAmount = 82076;
        }
        activeAmount = getActiveAmount(activeAmount);
        vo.setActiveAmount(getFinalNumeral(activeAmount));
        request.getSession().setAttribute("activeAmount", activeAmount);
        return Response.ok().data(vo);
    }

    private int getActiveAmount(Integer activeAmount) {
        int[] num = {-1, 1};
        int index = new Random().nextInt(2);
        int randomNum = new Random().nextInt(1000);
        int total = num[index] * randomNum + activeAmount;
        if (total < 50000) {
            return activeAmount + randomNum;
        } else if (total > 130000) {
            return activeAmount - randomNum;
        } else {
            return total;
        }
    }
}
